package com.jn.exam.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.exam.R;
import com.jn.exam.question.entity.*;
import com.jn.exam.question.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-01-18
 */
@RestController
@RequestMapping("/question/attribute")
@Api(description = "查询某些固定属性")
public class AttributeController {

    @Autowired
    private MajorService majorService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private PaperStatusService paperStatusService;

    @Autowired
    private PaperOperationPermissionService paperOperationPermissionService;


    @ApiOperation("根据名称模糊查询专业")
    @GetMapping("getMajorByName")
    public R getMajorByName(String name){
        QueryWrapper<Major> wrapper=new QueryWrapper<>();
        wrapper.like("major",name);
        List<Major> majors = majorService.list(wrapper);
        return R.ok().data("majors",majors);
    }

    @ApiOperation("根据名称模糊查询科目")
    @GetMapping("getSubjectByName")
    public R getSubjectByName(String name){
        QueryWrapper<Subject> wrapper=new QueryWrapper<>();
        wrapper.like("subject",name);
        List<Subject> subjects = subjectService.list(wrapper);
        return R.ok().data("subjects",subjects);
    }

    @ApiOperation("根据题型名称查询题型")
    @GetMapping("getTypeByName")
    public R getTypeByName(String name){
        QueryWrapper<Type> wrapper=new QueryWrapper<>();
        wrapper.like("type",name);
        List<Type> types = typeService.list(wrapper);
        return R.ok().data("types",types);
    }

    @ApiOperation(value = "获取所有专业map")
    @GetMapping("getMajorMap")
    @Cacheable(value = "major")
    public R getMajorMap(){
        List<Major> majorList= majorService.list(null);
        return R.ok().data("majorList",majorList);
    }

    @ApiOperation("增加一个专业")
    @PutMapping("addOneMajor")
    @CacheEvict(value = "major",allEntries = true)
    public R addOneMajor(String major){
        Major m=new Major();
        m.setMajor(major);
        boolean save = majorService.save(m);
        if (save) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation("修改一个专业名")
    @PostMapping("updateOneMajor")
    @CacheEvict(value = "major",allEntries = true)
    public R updateOneMajor(@RequestBody Major major){
        boolean update = majorService.updateById(major);
        if (update){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation(value = "获取所有科目map")
    @GetMapping("getSubjectMap")
    @Cacheable(value = "subject")
    public R getSubjectMap(){
        List<Subject> subjectList= subjectService.list(null);
        return R.ok().data("subjectList",subjectList);
    }

    @ApiOperation(value = "新增一个科目")
    @PutMapping("addOneSubject")
    @CacheEvict(value = "subject",allEntries = true)
    public R addOneSubject(String subject){
        Subject s=new Subject();
        s.setSubject(subject);
        boolean save = subjectService.save(s);
        if (save) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation("修改一个科目名")
    @PostMapping("updateOneSubject")
    @CacheEvict(value = "subject",allEntries = true)
    public R updateOneSubject(@RequestBody Subject subject){
        boolean update = subjectService.updateById(subject);
        if (update){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation(value = "获取所有题型map")
    @GetMapping("getTypeMap")
    @Cacheable(value = "type")
    public R getTypeMap(){
        List<Type> typeList= typeService.list(null);
        return R.ok().data("typeList",typeList);
    }

    @ApiOperation("新增一个题型")
    @PostMapping("addOneType")
    @CacheEvict(value = "type",allEntries = true)
    public R addOneType(String id,String type){
        boolean save = typeService.addOneType(id, type);
        if (save) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    @ApiOperation("修改一个题型")
    @PostMapping("updateOneType")
    @CacheEvict(value = "type",allEntries = true)
    public R updateOneType(@RequestBody Type type){
        boolean update = typeService.updateById(type);
        if (update){
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

    @ApiOperation("获取试卷的所有状态")
    @GetMapping("getPaperStatus")
    @Cacheable(value = "paperStatus")
    public R getPaperStatus(){
        QueryWrapper<PaperStatus> wrapper=new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List<PaperStatus> paperStatusList = paperStatusService.list(wrapper);
        return R.ok().data("paperStatusList",paperStatusList);
    }

    @ApiOperation("获取所有老师操作试卷的权限")
    @GetMapping("getPaperOperationPermission")
    @Cacheable(value = "paperOperationPermission")
    public R getPaperOperationPermission(){
        QueryWrapper<PaperOperationPermission> wrapper=new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List<PaperOperationPermission> paperOperationPermissionList = paperOperationPermissionService.list(wrapper);
        return R.ok().data("paperOperationPermissionList",paperOperationPermissionList);
    }

}

